﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Sonce.Controls
{
    public partial class Toolbar : ItemsControl
    {
        #region Members
        private Orientation orientation = Orientation.Horizontal;
        #endregion

        #region Constructor
        public Toolbar()
            : base()
        {
            this.DefaultStyleKey = typeof(Toolbar);
            this.Orientation = Orientation.Horizontal;
            this.VerticalAlignment = VerticalAlignment.Top;
            this.HorizontalAlignment = HorizontalAlignment.Left;
        }
        #endregion

        #region Properties
        public Orientation Orientation
        {
            get
            {
                return this.orientation;
            }
            set
            {
                this.orientation = value;
                if (ItemsContainer != null)
                {
                    try
                    {
                        switch (this.orientation)
                        {
                            case Orientation.Vertical:
                                this.ItemsPanel = (ItemsPanelTemplate)ItemsContainer.Resources["ItemsPanelVertical"];
                                break;

                            case Orientation.Horizontal:
                                this.ItemsPanel = (ItemsPanelTemplate)ItemsContainer.Resources["ItemsPanelHorizontal"];
                                break;
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }
        #endregion

        #region Template
        protected FrameworkElement RootElement;
        protected StackPanel ItemsContainer;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            RootElement = (FrameworkElement)GetTemplateChild("RootElement");
            ItemsContainer = (StackPanel)GetTemplateChild("ItemsContainer");
            this.Orientation = this.orientation;
        }
        #endregion

        #region Bubble events
        public event EventHandler<ToolbarEventArgs> ItemClick;
        internal virtual void OnItemClick(ToolbarEventArgs e)
        {
            if (ItemClick != null)
            {
                ItemClick(this, e);
            }
        }
        #endregion
    }

    public class ToolbarEventArgs : EventArgs
    {
        #region Properties
        public object OriginalSource
        {
            get;
            set;
        }
        #endregion

        #region Constructors
        public ToolbarEventArgs()
        {
        }

        public ToolbarEventArgs(object source)
        {
            this.OriginalSource = source;
        }
        #endregion
    }
}
